package com.frank.im.base;

import java.util.HashMap;
import java.util.Map;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

import com.frank.http.encryp.EncrypUtils;
import com.frank.im.base.ImService.ImLocalBinder;
import com.frank.im.utils.ImManageUtil;
import com.frank.im.utils.Logger;

/**
 * im服务连接类
 * 
 * @author Frank
 * @version 1.0 Create by 2016.4.8
 */
public class ImServiceConnection implements ServiceConnection, ImConnable {
	private final static String TAG = ImServiceConnection.class.getSimpleName();
	private ImService imService = null;
	private boolean isBound = false;
	private static ImServiceConnection connection;
	private Map<String, ImCallback> callBackHashMap = new HashMap<String, ImCallback>();
	private Map<String, String> operateHashMap = new HashMap<String, String>();
	private Map<String, String> contentHashMap = new HashMap<String, String>();
	private Map<String, String> typeHashMap = new HashMap<String, String>();

	private ImServiceConnection() {
	}

	public synchronized static ImServiceConnection getInstance() {
		if (connection == null) {
			connection = new ImServiceConnection();
		}
		return connection;
	}

	@Override
	public void onServiceConnected(ComponentName name, IBinder service) {
		if (imService == null && isBound == false) {
			imService = ((ImLocalBinder) service).getService();
			isBound = true;
			String requestId = "";
			ImCallback callback = null;
			String content = "";
			String type = "";
			if (operateHashMap != null && callBackHashMap != null) {
				requestId = operateHashMap.get("login");
				if (requestId != null && !requestId.equals("")) {
					callback = callBackHashMap.get(requestId);
					if (callback != null) {
						Logger.i(TAG, callback.toString());
						imService.login(requestId, callback);
						callBackHashMap.remove(requestId);
						operateHashMap.remove("login");
					}
				}
			}
			if (operateHashMap != null && callBackHashMap != null) {
				requestId = operateHashMap.get("logout");
				if (requestId != null && !requestId.equals("")) {
					callback = callBackHashMap.get(requestId);
					if (callback != null) {
						imService.login(requestId, callback);
						callBackHashMap.remove(requestId);
						operateHashMap.remove("logout");
					}
				}
			}
			if (operateHashMap != null && callBackHashMap != null
					&& contentHashMap != null && typeHashMap != null) {
				requestId = operateHashMap.get("content");
				if (requestId != null && !requestId.equals("")) {
					callback = callBackHashMap.get(requestId);
					content = contentHashMap.get(requestId);
					type = typeHashMap.get(requestId);
					if (callback != null && content != null) {
						imService.send(requestId, type, content, callback);
						callBackHashMap.remove(requestId);
						contentHashMap.remove(requestId);
						typeHashMap.remove(requestId);
						operateHashMap.remove("content");
					}
				}
			}
		}
	}

	@Override
	public void onServiceDisconnected(ComponentName name) {
		isBound = false;
	}

	@Override
	public synchronized void startImService(Context ctx) {
		if (!ImManageUtil.isServiceRunning(ctx, "com.hanzi.sbc.im.ImService")) {
			ctx.startService(new Intent(ctx, ImService.class));
		}
	}

	@Override
	public synchronized void login(Context ctx, ImCallback callback) {
		String requestId = EncrypUtils.getUUId();
		if (imService == null && isBound == false && connection != null) {
			if (operateHashMap != null) {
				operateHashMap.put("login", requestId);
			}
			if (callBackHashMap != null) {
				callBackHashMap.put(requestId, callback);
			}
			ctx.bindService(new Intent(ctx, ImService.class), connection,
					Context.BIND_AUTO_CREATE);
		} else if (imService != null && isBound == true && connection != null) {
			imService.login(requestId, callback);
		} else {
			Logger.i(TAG, "socket登录异常");
		}

	}

	@Override
	public synchronized void logout(Context ctx, ImCallback callback) {
		String requestId = EncrypUtils.getUUId();
		if (imService == null && isBound == false && connection != null) {
			if (operateHashMap != null) {
				operateHashMap.put("logout", requestId);
			}
			if (callBackHashMap != null) {
				callBackHashMap.put(requestId, callback);
			}
			ctx.bindService(new Intent(ctx, ImService.class), connection,
					Context.BIND_AUTO_CREATE);
		} else if (imService != null && isBound == true && connection != null) {
			imService.logout(requestId, callback);
			//ctx.unbindService(connection);
		} else {
		}
	}

	@Override
	public synchronized void send(Context ctx, String type, String content,
			ImCallback callback) {
		String requestId = EncrypUtils.getUUId();
		if (imService == null && isBound == false && connection != null) {
			if (operateHashMap != null) {
				operateHashMap.put("content", requestId);
			}
			if (callBackHashMap != null) {
				callBackHashMap.put(requestId, callback);
			}
			if (contentHashMap != null) {
				contentHashMap.put(requestId, content);
			}
			if (typeHashMap != null) {
				typeHashMap.put(requestId, type);
			}
			ctx.bindService(new Intent(ctx, ImService.class), connection,
					Context.BIND_AUTO_CREATE);
		} else if (imService != null && isBound == true && connection != null) {
			if (isLogin()) {
				imService.send(requestId, type, content, callback);
			}
		} else {
		}
	}

	public synchronized boolean isLogin() {
		if (imService != null && isBound == true && connection != null) {
			return imService.isLogin();
		}
		return false;
	}

	@Override
	public void unbindService(Context ctx) {
		if(connection!=null&&isBound == true){
			ctx.unbindService(connection);
			Logger.i(TAG, ""+connection);
			isBound = false;
		}
	}
}
